<?php
    include_once('session.php');
    require_once('php/class/edificios.class.php');
    
    //viene a modificar?
    if(isset($_GET['i'])){
        $id_edificio = $_GET['i'];
        $oEdificio = new edificio();
        $oEdificio->search($id_edificio);
        $nombre = $oEdificio->NOMBRE;
        $id_empresa = $oEdificio->ID_EMPRESA;
        $costo_envio = $oEdificio->COSTO_ENVIO;
        $direccion = $oEdificio->DIRECCION;
        $telefono = $oEdificio->TELEFONO;
        $mail = $oEdificio->MAIL;
        
        $lunes = $oEdificio->LUNES;
        if(strlen($lunes)>0){
			$d1="checked";
			}else{
			$d1="";
			}
        $martes = $oEdificio->MARTES;
			if(strlen($martes)>0){
			$d2="checked";
			}else{
			$d2="";
			}
        $miercoles = $oEdificio->MIERCOLES;
			if(strlen($miercoles)>0){
			$d3="checked";
			}else{
			$d3="";
			}
        $jueves = $oEdificio->JUEVES;
			if(strlen($jueves)>0){
			$d4="checked";
			}else{
			$d4="";
			}
        $viernes = $oEdificio->VIERNES;
			if(strlen($viernes)>0){
			$d5="checked";
			}else{
			$d5="";
			}
		$sabado = $oEdificio->SABADO;
			if(strlen($sabado)>0){
			$d6="checked";
			}else{
			$d6="";
			}
		$domingo = $oEdificio->DOMINGO;
			if(strlen($domingo)>0){
			$d7="checked";
			}else{
			$d7="";
			}
        
        $accion="MODIFY";
        
    }else{
       //variables
        $nombre = "";
        $id_empresa = "";
        $direccion = "";
        $telefono = "";
        $mail = "";
        $accion = "ADD";
        $id_edificio = "";
        $costo_envio = "";
        $lunes = "";
        $martes = "";
        $miercoles = "";
        $jueves = "";
        $viernes = "";
        $d1 = "";
        $d2 = "";
        $d3 = "";
        $d4 = "";
        $d5 = "";
        $d6 = "";
        $d7 = "";
    }
    
    //las empresas:
    require_once('php/class/empresas.class.php');
    $oEmpresa = new empresa();
    $las_empresas = @$oEmpresa->buscar();
    
        if($las_empresas==0){
            $lista_empresas = "No hay nada que ver...";
        }else{
            $lista_empresas = "<select id='id_empresa' style='width:100%' name='id_empresa' class='validate[\"required\"]'>";
            $lista_empresas .= "<option value=-1 selected>Seleccione una</option>";
            
                for($i=0;$i<count($las_empresas['ID']);$i++){
                    if($las_empresas['ID'][$i]==$id_empresa){
                        $selected = "selected";
                    }else{
                        $selected ="";
                    }
                    $lista_empresas .= "<option value=".$las_empresas['ID'][$i]." ".$selected.">".$las_empresas['NOMBRE'][$i]."</option>";
                    
                }
            $lista_empresas .= "</select>";
        }
    
    
    
?>
<html>
    <?php include('include.header_admin.html'); ?>
    <body class="bodyadmin">
        <div id="articulo" align="center">
            
            <form id="frm_bp" name="frm_bp" method="post">
                <input type="hidden" id="ACCION" name="ACCION" value="<?php echo $accion;?>">
                <input type="hidden" id="id" name="id" value="<?php echo $id_edificio;?>">
                <table class="marco">
                <tr>
                <td height="10%">
                <?php include('menu.php'); ?>
                </td>
                </tr>
		    <tr height="5%">
			<td align="center" valign="button">
			    Registro de edificios
			</td>
		    </tr>
		    <tr>
			<td align="center" valign="middle">
                <div id="DATO_HIDE" style="display:none" style="width:90%">
                    <div id="DATO"></div>
                </div>
            <table class="formulario">
                <tr>
                    <td width="100px">Empresa</td>
                    <td width="200px"><?php echo $lista_empresas;?></td>
                    <td colspan="2" align="center">D&iacute;as a visitar el edificio</td>
                    </tr>
                <tr>
                    <td>Edificio</td>
                    <td><input type="text" id="nombre" name="nombre" style="width:100%" class="validate['required']" value="<?php echo $nombre?>"></td>
					<td width="100px"><input type="checkbox" id="d1" <?php echo $d1 ?> name="d1" value="1" onChange="dia(this);">&nbsp;Lunes</td>
                    <td width="200px"><input type='text' style="display:none" id="lunes" name="lunes" value="<?php echo $lunes ?>"></td>
                </tr>
                <tr>
                    <td>Costo de env&iacute;o</td>
                    <td><input type="text" id="costo_envio" name="costo_envio" style="width:100%" class="validate['required','number']" value="<?php echo $costo_envio?>"></td>
					<td><input type="checkbox" id="d2" name="d2" value="2" <?php echo $d2 ?> onChange="dia(this);">&nbsp;Martes</td>
                    <td><input type='text' style="display:none" id="martes" name="martes" value="<?php echo $martes ?>"></td>
                </tr>
                <tr>
                    <td>Tel&eacute;fono</td>
                    <td><input type="text" id="telefono" name="telefono" style="width:100%" class="validate['required','phone']" value="<?php echo $telefono;?>"></td>
					<td><input type="checkbox" id="d3" name="d3" value="3" <?php echo $d3 ?> onChange="dia(this);">&nbsp;Miercoles</td>
                    <td><input type='text' style="display:none" id="miercoles" name="miercoles" value="<?php echo $miercoles ?>"></td>
                </tr>
                <tr>
                    <td>Mail</td>
                    <td><input type="text" id="mail" name="mail" style="width:100%" class="validate['required','email']" value="<?php echo $mail;?>"></td>
					<td><input type="checkbox" id="d4" name="d4" value="4" <?php echo $d4 ?> onChange="dia(this);">&nbsp;Jueves</td>
                    <td><input type='text' style="display:none" id="jueves" name="jueves" value="<?php echo $jueves ?>"></td>
                </tr>
                <tr>
                    <td rowspan="3" valign="top">Direcci&oacute;n</td>
                    <td rowspan="3" valign="top"><textarea id="direccion" name="direccion" style="width:100%" class="validate['required','length[1,200]']"><?php echo $direccion?></textarea></td>
					<td><input type="checkbox" id="d5" name="d5" value="5" <?php echo $d5 ?> onChange="dia(this);">&nbsp;Viernes</td>
                    <td><input type='text' style="display:none" id="viernes" name="viernes" value="<?php echo $viernes ?>"></td>
                </tr>
                <tr>
					<td><input type="checkbox" id="d6" name="d6" value="6" <?php echo $d6 ?> onChange="dia(this);">&nbsp;Sabado</td>
                    <td><input type='text' style="display:none" id="sabado" name="sabado" value="<?php echo $sabado ?>"></td>
                </tr>
                <tr>
					<td><input type="checkbox" id="d7" name="d7" value="7" <?php echo $d7 ?> onChange="dia(this);">&nbsp;Domingo</td>
                    <td><input type='text' style="display:none" id="domingo" name="domingo" value="<?php echo $domingo ?>"></td>
                </tr>
                <tr>
                    <td></td>
                    <td align="right">
                        <input type="button" onclick="javascript:document.location='list_edificios.php';" id="cancel" name="cancel" value="Cancelar">
                    </td>
                    <td align="left">
                        <input type="submit" id="submit" name="submit" value="Guardar">
                    </td>
                    <td></td>
                </tr>
            </table>
                        </td>
                    </tr>
                    <tr>
			<td height="10%" valign="bottom" align="center">
			<?php include('footer.php'); ?>
			</td>
			</tr>
                </table>
            </form>
             
        </div>
    </body>
    <?php include('include.footer_admin.html'); ?>
   <script>
   
   dia($('d1'));
   dia($('d2'));
   dia($('d3'));
   dia($('d4'));
   dia($('d5'));
   dia($('d6'));
   dia($('d7'));
   
   function dia(dia){
	   if (dia.checked){
		   switch (dia.value){
		   case "1":
		   $('lunes').show();
		   break;
		   
		   case "2":
		   $('martes').show();
		   break;
		   
		   case "3":
		   $('miercoles').show();
		   break;
		   
		   case "4":
		   $('jueves').show();
		   break;
		   
		   case "5":
		   $('viernes').show();
		   break;
		   
		   case "6":
		   $('sabado').show();
		   break;
		   
		   case "7":
		   $('domingo').show();
		   break;
		   }
	   
	   }else{
		   
		   switch (dia.value){
		   case "1":
		   $('lunes').hide();
		   break;
		   
		   case "2":
		   $('martes').hide();
		   break;
		   
		   case "3":
		   $('miercoles').hide();
		   break;
		   
		   case "4":
		   $('jueves').hide();
		   break;
		   
		   case "5":
		   $('viernes').hide();
		   break;
		   
		   case "6":
		   $('sabado').hide();
		   break;
		   
		   case "7":
		   $('domingo').hide();
		   break;
		   }
		}
	}
    ///////////////////////////////
    var myCheck;
	myCheck = new FormCheck('frm_bp', {
	  submit:false,
		display:{
			errorsLocation : 1,
			indicateErrors : 2,
			keepFocusOnError : 0,
			closeTipsButton : 1,
			checkValueIfEmpty:1,                
			showErrors : 1
		}
	});		 
 
    $('frm_bp').addEvent('submit',function(ev){
        var IsValid;
       IsValid=myCheck.isFormValid();                                                                          
        if (IsValid){ 
            
             Ejecutor();
        }    
    });
    ///////////////////////////////

    function Ejecutor(){        
        window.addEvent('domready', function() {      	       
    		var req = new Request.JSON({
                method: 'POST',
                url: 'ajax/ajax_edificio.php', 
                data: $('frm_bp'),                
                noCache:true,                     
                onRequest: function(){
                    $('submit').disabled = true;
                    $('DATO_HIDE').show();
                    $('DATO').innerHTML = 'Procesando ...'; 
                },
                onComplete: function(){
                    setTimeout(function(){
                        $('DATO_HIDE').fade('out').get('tween').chain(function() {
                            $('DATO_HIDE').hide();
                            $('DATO_HIDE').fade('in');                                                         
                        });
                    },2000);                                                                                                                                                                                                              
                },             
                onSuccess: function(response){
                    switch(parseInt(response.RESULT)){
                        case 0:
                            $('DATO').innerHTML = "Operaci&oacute;n exitosa";  
                            window.location='list_edificios.php';
                            break;
                        case 1:
                            $('DATO').innerHTML = response.ERROR;
                            $('submit').disabled = false;
                            break;
                    }
                }
            }).get();        
       });
    }
    </script>
</html>
